The UnilSports makes it easier for UNIL/EPFL users or people interested in the activities of the University of Lausanne Sports Center to find their preferred sport by optimizing the time schedules(if some of the sports selected overlap, this package will handle it). In addition, from the selected sports and time schedules the optimum value of calorie burn will be selected, so the user can get as an output the most efficient sport schedule, calorie burn and time duration.
This document introduces you to UnilSports’ basic set of tools and show how to apply them in the datasets provided. Furthermore, I will guide you across the several functions, so that you are able to run the shiny application for the interactive visualizations.
library(UnilSports)This is one of the main points to understand, in order to use this
package. Due to the dependency of the information from the Unil Sports
center website, we had to consider and automatic way for the retrieval
of this information. Therefore, we have added these functions
webscrape_sports and webscrape_MET for this
automatic task that will be later explained.
The data sets available in this package are related to some of the functions as we have explained. Let’s go through each of them so it is more clear.
This dataset contains the sports activities from the Unil Sports center for a week. It will depend on the day that was computed, so it may be a different date that appears for you but the structure remains the same.
To know about what represent each columns please use this command in
the console. ?sport_schedule
# To load the data
UnilSports::sport_schedule
#> # A tibble: 506 × 4
#> Date Timetable Activity Location
#> <date> <chr> <chr> <chr>
#> 1 2022-12-14 07:00 – 08:00 Réveil musculaire (Cours hybride) CSS-Salle …
#> 2 2022-12-14 08:00 – 09:00 Football / Pratique libre Rugby-Foot…
#> 3 2022-12-14 08:00 – 09:00 Basketball / Pratique libre à l'extérieur Basket Ent…
#> 4 2022-12-14 08:00 – 09:00 Football / Pratique libre Terrain Ce…
#> # … with 502 more rowsThis dataset contains the metabolic equivalent of task (MET) of each
exercise activity (821 sub-activities in total). These values will be
use on the optimize_schedule function, so it can compute
the calorie burn per activity.
To find out what each column represents, use the
?met_values command in the console.
# To load the data
UnilSports::met_values
#> # A tibble: 821 × 3
#> Activity `Specific Motion` METs
#> <chr> <chr> <dbl>
#> 1 bicycling bicycling, mountain, uphill, vigorous 14
#> 2 bicycling bicycling, mountain, competitive, racing 16
#> 3 bicycling bicycling, BMX 8.5
#> 4 bicycling bicycling, mountain, general 8.5
#> # … with 817 more rowsThis dataset contains a table with all the activity names on the UNIL
Sport center and the sub-activity/specific motion names in the MET
values table. From the name we can have an idea of the purpose of the
dataset. This table will mapped the sport activities with the MET valus
when using the get_cleanschedule_met function.
To learn the meaning of each column, enter the ?mapping
command in the console.
# To load the data
UnilSports::mapping
#> # A tibble: 171 × 2
#> Activity `Specific Motion`
#> <chr> <chr>
#> 1 Musculation connectée / 1. Introduction health club exercise classes, gener…
#> 2 Tai ji quan / Tous niveaux video exercise workouts, TV conditi…
#> 3 Basketball / Pratique libre à l'extérieur basketball, general
#> 4 Football / Pratique libre football, touch, flag, general
#> # … with 167 more rowsThis dataset is linked to the previous datasets, as it contain the information from the three of them. From this dataset we will be able to use the functions related to the shiny application, as it is the one containing all the information needed.
To understand the significance of each column, use the
?clean_sport_schedule command in the console.
# To load the data
UnilSports::clean_sport_schedule %>% head(5)
#> Date Start time End time Duration_min
#> 1 2022-12-14 7 8 60
#> 2 2022-12-14 8 9 60
#> 3 2022-12-14 8 9 60
#> 4 2022-12-14 8 9 60
#> 5 2022-12-14 8 9 60
#> Activity
#> 1 Réveil musculaire (Cours hybride)
#> 2 Football / Pratique libre
#> 3 Basketball / Pratique libre à l'extérieur
#> 4 Football / Pratique libre
#> 5 Football / Pratique libre
#> Location METs p
#> 1 CSS-Salle polyvalente 3.0 3.150
#> 2 Rugby-Foot Chamberonne 1/2 terrain nord 8.0 8.400
#> 3 Basket Entier 6.5 6.825
#> 4 Terrain Central Ouest 8.0 8.400
#> 5 Terrain Central Est 8.0 8.400Now, that we know a bit more about the datasets, we will be able to use the functions. We have 7 functions within this package that we will be describe on this section. Let’s have some fun!
webscrape_sports()webscrape_sports() allows you to web scrap the UNIL’s Sport Center website for the
amount of days you want to know the sports schedules. The return output
is a data frame containing the weekly itinerary for the activity
sports.
Note: The UNIL’s Sport website do not have all the activities if we try to get a date period above a week, for that reason we have consider 7 days. But it is feasible to retrieve more than on week a head.
# Create Sport Schedule dataset
sport_schedule <- webscrape_sports(days = 7)
head(sport_schedule,5) # Visualize the first 5 rows of the data frame
#> # A tibble: 5 × 4
#> Date Timetable Activity Location
#> <date> <chr> <chr> <chr>
#> 1 2022-12-19 08:00 – 09:00 Basketball / Pratique libre à l'extérieur Basket Ent…
#> 2 2022-12-19 08:00 – 09:00 Football / Pratique libre Rugby-Foot…
#> 3 2022-12-19 08:00 – 09:00 Football / Pratique libre Rugby-Foot…
#> 4 2022-12-19 09:00 – 10:00 Basketball / Pratique libre à l'extérieur Basket Ent…
#> # … with 1 more row
# or without the parameter
sport_schedule <- webscrape_sports()
head(sport_schedule,5) # Visualize the first 5 rows of the data frame
#> # A tibble: 5 × 4
#> Date Timetable Activity Location
#> <date> <chr> <chr> <chr>
#> 1 2022-12-19 08:00 – 09:00 Basketball / Pratique libre à l'extérieur Basket Ent…
#> 2 2022-12-19 08:00 – 09:00 Football / Pratique libre Rugby-Foot…
#> 3 2022-12-19 08:00 – 09:00 Football / Pratique libre Rugby-Foot…
#> 4 2022-12-19 09:00 – 10:00 Basketball / Pratique libre à l'extérieur Basket Ent…
#> # … with 1 more rowwebscrape_MET()webscrape_MET() support the user to web scrap the MET
values from the Procon.org
website. As we have mentioned before this will allow the
get_cleanschedule_met() function for the mapping of sports
activities and MET values. The return output is a data frame containing
the MET values of more than 800 activities.
# Create MET values dataset
met_values <- webscrape_MET()
head(met_values,10) # Visualize the first 10 rows of the data frame
#> # A tibble: 10 × 3
#> Activity `Specific Motion` METs
#> <chr> <chr> <dbl>
#> 1 bicycling bicycling, mountain, uphill, vigorous 14
#> 2 bicycling bicycling, mountain, competitive, racing 16
#> 3 bicycling bicycling, BMX 8.5
#> 4 bicycling bicycling, mountain, general 8.5
#> # … with 6 more rowsget_cleanschedule_met()get_cleanschedule_met() let you to link the previous
data frames (met_values, sport_schedule)
created to prepare the mapping of sports activities and MET values for
the optimization function optimize_schedule(). The return
output contains a mapped dataframe containing sports activities, MET
values and the number of calories burned per weight (kg).
# Clean and Link the datasets
clean_sport_schedule <- get_cleanschedule_met(sport_schedule,met_values)
head(clean_sport_schedule,5) # Visualize the first 5 rows of the data frame
#> Date Start time End time Duration_min
#> 1 2022-12-19 8 9 60
#> 2 2022-12-19 8 9 60
#> 3 2022-12-19 8 9 60
#> 4 2022-12-19 9 10 60
#> 5 2022-12-19 9 10 60
#> Activity
#> 1 Basketball / Pratique libre à l'extérieur
#> 2 Football / Pratique libre
#> 3 Football / Pratique libre
#> 4 Basketball / Pratique libre à l'extérieur
#> 5 Football / Pratique libre
#> Location METs p
#> 1 Basket Entier 6.5 6.825
#> 2 Rugby-Foot Chamberonne 1/2 terrain nord 8.0 8.400
#> 3 Rugby-Foot Chamberonne 1/2 terrain sud 8.0 8.400
#> 4 Basket Entier 6.5 6.825
#> 5 Rugby-Foot Chamberonne 1/2 terrain nord 8.0 8.400optimize_schedule()optimize_schedule() allows you to evaluate the calorie
burn per activity and time according to the parameters entered. The main
objective is to minimize the number of chosen activities. \[min\sum_{i=1}^N x_i\] where \(x_i\) is a binary variable that is 1 if
activity \(i\) is chosen and 0 if it is
not chosen.
This optimization function calculates the calorie burn subject to 3 constraints:
The total calorie burn must exceed the target calorie: \[\sum_{i=1}^N (x_i \cdot cal_i) \geq calburn\] where \(cal_i\) is the calorie burn of activity \(i\) and \(calburn\) is the target calorie.
No overlapping time slots: We set constraints to ensure that no more than one activity can be selected for any overlapping time interval. For example, if activity A starts at 8.00 and ends at 9.00 and activity B starts at 8.45 and ends at 9.15, they cannot be selected together (i.e. only one of them can be selected). \[\sum_{i \in A} x_i + \sum_{i \in B} x_i + \sum_{i \in C} x_i \leq 1\] for all overlapping time intervals where activity \(A\), \(B\), \(C\), … have overlapping time slots.
for all duplicate activities where activity \(I\), \(J\), \(K\), … are the same activity.
To understand the significance of each parameter, use the
?optimize_schedule() command in the console.
# Amount of calories you want to burn
calburn <- 500
# Date preference - keep the same format*
date <- c('2022-12-14')
# Sport activity of interest
# The activity has to appear on the date selected
activity <- c('Football / Pratique libre','Badminton / Tous niveaux',
'Aquagym', 'Zumba', 'Pilates', 'Agrès',
'Salsa cubaine / Débutants')
# Your current weight(kg)
weight <- 50
# Time availability
time <- c('07:00 \u2013 19:00')
# 1 if you want different activities, 0 otherwise
flag_no_duplicate_activities <- 1
# Load the clean_sport_schedule from the package
clean_sport_schedule <- UnilSports::clean_sport_schedule
# Apply optimize_schedule() function
optimize_output <- optimize_schedule(clean_sport_schedule, date, activity,
time, calburn, weight,
flag_no_duplicate_activities)
#> The optimization is successful
# Call function and see the results
head(optimize_output$table_result,10)
#> Date Start time End time time Duration_min Activity
#> 1 2022-12-14 8 9 1 60 Football / Pratique libre
#> 13 2022-12-14 11 12 1 60 Pilates
#> Location METs p calburn
#> 1 Rugby-Foot Chamberonne 1/2 terrain nord 8 8.40 420.0
#> 13 CSS-Salle polyvalente 3 3.15 157.5pie_optim()pie_optim() let the user print a pie chart reflecting
the output of the optimization function. This function will be use
inside the Shiny app function logic.
# Amount of calories you want to burn
calburn <- 500
# Date preference - keep the same format*
date <- c('2022-12-14')
# Sport activity of interest
# The activity has to appear on the date selected
activity <- c('Football / Pratique libre','Badminton / Tous niveaux',
'Aquagym', 'Zumba', 'Pilates', 'Agrès',
'Salsa cubaine / Débutants')
# Your current weight(kg)
weight <- 50
# Time availability
time <- c('07:00 \u2013 19:00')
# 1 if you want different activities, 0 otherwise
flag_no_duplicate_activities <- 1
# Load the clean_sport_schedule from the package
clean_sport_schedule <- UnilSports::clean_sport_schedule
# Run the function
optimize_output <- optimize_schedule(clean_sport_schedule, date, activity,
time, calburn, weight,
flag_no_duplicate_activities)
#> The optimization is successful
optim_plot <- optimize_output$table_result
# Call function
pie_optim(optim_plot)
#> [[1]]
#>
#> [[2]]startApp()startApp() allows the users to run automatically a
graphical user interface of the optimization tool. Here users are going
to be able to interact and get the time schedules of the preferred
sports activities with the optimal calorie burn.
The return output should be the shiny application of the Sports Unil Plan
# Call the function
startApp()